package com.qf.servlet;

import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCommentService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.servlet
 * @Description:
 * @Date 2022/10/18 15:20
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/admin/comment")
public class AdminCommentController {

    @Autowired
    DtsBrandService dtsBrandService;

    @Autowired
    DtsCommentService dtsCommentService;

    /**
     * 商品评论的查询
     */
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",defaultValue = "20")Integer limit,
                       @RequestParam(value = "sort",defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",defaultValue = "desc") String order,
                       Integer userId,Integer valueId){

        //获取用户，判断用户是否是品牌制造商
        Subject subject = SecurityUtils.getSubject();

        List<Integer> brandIds = null;
        if(subject.hasRole("品牌制造商")){
            DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
            List<DtsBrand> byAdminId = dtsBrandService.findByAdminId(admin.getId());
            //获取品牌ID
            brandIds = byAdminId.stream().map(dtsBrand -> {
                return dtsBrand.getId();
            }).collect(Collectors.toList());
        }

        //不是品牌制造商就查询全部的评论
        Map<String,Object> data =dtsCommentService.findByPage(page,limit,sort,order,userId,valueId,brandIds);

        return ResponseUtil.ok(data);
    }

    /**
     * 评论删除
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsComment dtsComment){

        dtsCommentService.deleteById(dtsComment);

        return ResponseUtil.ok();
    }
}


